package guoanmaker.operator.business.controller;

import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import guoanmaker.operator.business.model.InRoundPhone;
import guoanmaker.operator.business.model.OperatorHotwords;
import guoanmaker.operator.business.service.OperatorHotwordsServiceInterface;
import guoanmaker.operator.helper.common.AjaxJson;
import guoanmaker.operator.helper.common.abstractobj.ApiController;
import io.swagger.annotations.ApiOperation;

/**
 *  
 *
 * @author huZhaoyu 
 * @ClassName: OperatorHotwordsController 
 * @Description: OperatorHotwordsController
 * @date 2017/6/27
 */
@RestController
@RequestMapping(ApiController.OPERATOR_HOTWORDS_URL)
public class OperatorHotwordsController {

    private final OperatorHotwordsServiceInterface operatorHotwordsServiceInterface;

    @Autowired
    public OperatorHotwordsController( OperatorHotwordsServiceInterface operatorHotwordsServiceInterface) {
        this.operatorHotwordsServiceInterface = operatorHotwordsServiceInterface;
    }

    /**
     * @return AjaxJson   
     * @Title: saveHotwords 
     * @Description: 保存热词
     * @param: OperatorHotwords
     * @throws 
     */
    @ApiOperation(value = "保存热词", notes = "保存热词，包括新增与修改 ")
    @PostMapping(value = "saveHotwords")
    public AjaxJson saveHotwords(@RequestBody InRoundPhone inRoundPhone) {
    	AjaxJson ajaxJson=new AjaxJson();
    	OperatorHotwords[] hots=inRoundPhone.getHotwords();
    	try{
    	for(OperatorHotwords hot:hots){
    		hot.setCreateDate(new Date());
    		hot.setCityCode(inRoundPhone.getCityId());
    		operatorHotwordsServiceInterface.saveHotwords(hot);
    	}
    	OperatorHotwords wo=inRoundPhone.getHotword();
    	wo.setCreateDate(new Date());
    	wo.setCityCode(inRoundPhone.getCityId());
         operatorHotwordsServiceInterface.saveHotwords(wo);
         ajaxJson.setSuccess(true).setStatus(200);
    	}catch(Exception e){
    		ajaxJson.setSuccess(false).setStatus(250);
    	}
        
        return ajaxJson;
    }

    /**
     * @return AjaxJson   
     * @Title: deleteHotWords 
     * @Description: 删除热词
     * @param: String id
     * @throws 
     */
    @ApiOperation(value = "删除热词", notes = "根据Id删除热词")
    @PostMapping(value = "deleteHotwords")
    
    public AjaxJson deleteHotWords(@PathVariable(value = "id") String id) {
    	AjaxJson ajaxJson=new AjaxJson();
        operatorHotwordsServiceInterface.deleteHotwordById(id);
        return ajaxJson.setSuccess(true).setStatus(200).setData("删除数据成功!");
    }

    /**
     * @return AjaxJson   
     * @Title: getHotwordById 
     * @Description: 根据Id查找热词
     * @param: String id
     * @throws 
     */
    @ApiOperation(value = "根据Id查找热词", notes = "根据Id查找热词")
    @PostMapping(value = "getHotwordById")
  
    public AjaxJson getHotwordById(@RequestParam(value = "id") String id) {
    	AjaxJson ajaxJson=new AjaxJson();
        return ajaxJson.setSuccess(true).setStatus(200).setData(operatorHotwordsServiceInterface.getHotwordById(id));
    }

    /**
     * @return AjaxJson   
     * @Title: getAllHotwords 
     * @Description: 查询所有热词
     * @param: void
     * @throws 
     */
    @ApiOperation(value = "查询所有热词", notes = "查询所有热词")
    
    @PostMapping(value = "getAllHotwords")
    public AjaxJson getAllHotwords(){
    	AjaxJson ajaxJson=new AjaxJson();
        return ajaxJson.setSuccess(true).setStatus(200).setData(operatorHotwordsServiceInterface.getAllHotwords());
    }

    /**
     * @return AjaxJson   
     * @Title: getAllHotwordsByPage 
     * @Description: 查询所有热词并分页
     * @param: Integer page 页码
     * @param: Integer size 条数
     * @throws 
     */
    @ApiOperation(value = "查询所有热词并分页", notes = "查询所有热词并分页")
   
    @PostMapping(value = "getAllHotwordsByPage")
    public AjaxJson getAllHotwordsByPage(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                         @RequestParam(value = "size", defaultValue = "10") Integer size) {
    	AjaxJson ajaxJson=new AjaxJson();
    	return ajaxJson.setSuccess(true).setStatus(200).setData(operatorHotwordsServiceInterface.getAllHotwordsByPage(page, size));
    }
  /**
   * app首页  
  * @Title: getAllHotwordsByCityCode 
  * @Description: TODO(这里用一句话描述这个方法的作用) 
  * @param cityCode
  * @return    设定文件 
  * @return AjaxJson    返回类型 
  * @throws 
   */
    @ApiOperation(value = "根据城市id查询所有热词", notes = "根据城市id查询所有热词")
    @PostMapping(value = "getAllHotwordsByCityCode")
    public AjaxJson getAllHotwordsByCityCode(String cityCode){
    	AjaxJson ajaxJson=new AjaxJson();
    	Map<String, Object> reMap=null;
    	try{
    	reMap=operatorHotwordsServiceInterface.getAllHotwordsByCityId(cityCode);
    	ajaxJson.setSuccess(true).setStatus(200).setData(reMap);
    	}catch(Exception e){
    		ajaxJson.setSuccess(false).setStatus(250);
    	}
        return ajaxJson;
    }
    /**
     * pc首页
    * @Title: getAllHotwordsByCityCodePc 
    * @Description: TODO(这里用一句话描述这个方法的作用) 
    * @param cityCode
    * @return    设定文件 
    * @return AjaxJson    返回类型 
    * @throws 
     */
    @ApiOperation(value = "根据城市id查询所有pc热词", notes = "根据城市id查询所有pc热词")
    @PostMapping(value = "getAllHotwordsByCityCodePc")
    public AjaxJson getAllHotwordsByCityCodePc(String cityCode){
    	AjaxJson ajaxJson=new AjaxJson();
    	Map<String, Object> reMap=null;
    	try{
    	reMap=operatorHotwordsServiceInterface.getAllHotwordsByCityIdPc(cityCode);
    	ajaxJson.setSuccess(true).setStatus(200).setData(reMap);
    	}catch(Exception e){
    		ajaxJson.setSuccess(false).setStatus(250);
    	}
        return ajaxJson;
    }
}
